package dyyx.objenesis;

import java.io.Serializable;

import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.ObjenesisSerializer;
import org.springframework.objenesis.ObjenesisStd;

public class Hello {
//public class Hello implements Serializable{
	
	
	private final String name;
	
	/*
	public Hello() {
		this.name = "cat";
	}
	*/
	
	
	public Hello(String name) {
		this.name = name;
	}
	
	public void show() {
		System.out.println("name="+name);
	}

	public static void main(String[] args) throws Exception {
		
        Objenesis objenesis = new ObjenesisStd(true);
        Hello hello = objenesis.newInstance(Hello.class);
		System.out.println("hello1="+hello);
        hello.show();
        
        hello = objenesis.newInstance(Hello.class);
		System.out.println("hello2="+hello);
        hello.show();
        
        objenesis = new ObjenesisSerializer(true);
        // 需要 实现 Serializable ,否则抛异常
        //  java.io.NotSerializableException: class dyyx.objenesis.Hello not serializable
        hello = objenesis.newInstance(Hello.class);
		System.out.println("hello3="+hello);
        hello.show();
        
        hello = objenesis.newInstance(Hello.class);
		System.out.println("hello4="+hello);
        hello.show();
        
        
        // 没有默认构造函数 报错
        // java.lang.NoSuchMethodException: dyyx.objenesis.Hello.<init>()

        hello = (Hello)Class.forName("dyyx.objenesis.Hello").newInstance();
        hello.show();

		
	}
}
